/**
 * 
 */
package com.application.DataProvider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.application.DTO.FeedbackFeelDTO;

/**
 * @author TAI
 *
 */
public class FeedbackFeelDAO {

	Context _mcontext = null;

	public FeedbackFeelDAO(Context context){
		this._mcontext = context;
	}

		/**
	 * Insert Feedback
	 * @param fb_dto
	 * @return
	 */
	
	public boolean isExist(int idDetailLearning, int idLearner)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Feedback where IDDetailLearning=" +  idDetailLearning + " and IDLearner = " + idLearner;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	public int insertFeedbackFeel(FeedbackFeelDTO fbfDto) {
		Log.d("PEERDROID","insert Feedback");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("IDFeelChoice", fbfDto.get_IDFeelChoice());
			values.put("IDDetailLearning", fbfDto.get_IDDetailLearning());
			values.put("IDLearner", fbfDto.get_IDLearner());

			result = provider.execInsertCommand(db, "FeedbackFeel", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	/**
	 * Update Feedback
	 * @param fbfDto
	 * @return
	 */
	public int updateFeedbackFeel(FeedbackFeelDTO fbfDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("IDFeelChoice", fbfDto.get_IDFeelChoice());

			String whereClause="idlearner =? and iddetaillearning =?";
			String[]whereArgs = {String.valueOf(fbfDto.get_IDLearner()), String.valueOf(fbfDto.get_IDDetailLearning())};
			result= provider.execUpdateCommand(db, "FeedbackFeel", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}

		return result;
	}
	/**
	 * Delete Feedback
	 * @param idFeedback
	 * @return
	 */
	public int deleteFeedbackFeel(int idLearner, int idDetailLearning )
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="idlearner =? and iddetaillearning =?";
			String[]whereArgs = {String.valueOf(idLearner), String.valueOf(idDetailLearning)};
			result=provider.execDeleteCommand(db, "FeedbackFeel", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}
}
